package text.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.net.URL;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.SoftBevelBorder;

public class Viewer {

	// The preferred size of the demo
	private int PREFERRED_WIDTH = 680;
	private int PREFERRED_HEIGHT = 600;

	Border loweredBorder = new CompoundBorder(new SoftBevelBorder(SoftBevelBorder.LOWERED), 
						  new EmptyBorder(5,5,5,5));

	private GUI gui = null;
	private JFrame frame = null;
	private JPanel panel = null;
	private String resourceName = null;
	private String iconPath = null;
	private String sourceCode = null;

	// Resource bundle for internationalized and accessible text
	private ResourceBundle bundle = null;

	public Viewer(GUI gui) {
		this(gui, null, null);
	}

	public Viewer(GUI gui, String resourceName, String iconPath) {
//		UIManager.put("swing.boldMetal", Boolean.FALSE);
		panel = new JPanel();
		panel.setLayout(new BorderLayout());
	
		this.resourceName = resourceName;
		this.iconPath = iconPath;
		this.gui = gui;
	}

	public String getResourceName() {
		return resourceName;
	}

	public JPanel getViewerPanel() {
		return panel;
	}

	public JFrame getViewerFrame() {
		return frame;
	}

	private void setViewerFrame(JFrame frame) {
		this.frame = frame;
	}

	public GUI getGUI() {
		return gui;
	}

	public String getString(String key) {
		String value = "";
		if(bundle == null) {
			if(getGUI() != null) {
				bundle = getGUI().getResourceBundle();
			} else {
				bundle = ResourceBundle.getBundle("resources.gui");
			}
		}
		try {
			value = bundle.getString(key);
		} catch (MissingResourceException e) {
			System.out.println("java.util.MissingResourceException: Couldn't find value for: " + key);
		}
		return value;
	}
	
	public char getMnemonic(String key) {
		return (getString(key)).charAt(0);
	}
	
	public ImageIcon createImageIcon(String filename, String description) {
		URL url = null;
		ImageIcon icon = null;
		if(getGUI() != null) {
			icon = getGUI().createImageIcon(filename, description);
		} else {
			String path = "/resources/images/" + filename;
			try{
				url = getClass().getResource(path);
			} catch (MissingResourceException e) {
				System.out.println("java.util.MissingResourceException: Couldn't find image in: " + path);
			}
			if(url != null) {
				icon = new ImageIcon(url, description); 
			}else {
				System.out.println("java.util.MissingResourceException: Couldn't find image in: " + path);
			}
		}
		return icon;
	}
	
	public String getName() {
		return getString(getResourceName() + ".name");
	};

	public Image getImageIcon() {
		Image image = null;
		ImageIcon imageIcon = createImageIcon(iconPath, getToolTip());
		if(imageIcon != null){
			image = imageIcon.getImage();
		}
		return image;

	};

	public Icon getIcon() {
		return createImageIcon(iconPath, getToolTip());
	};

	public String getToolTip() {
		return getString(getResourceName() + ".tooltip");
	};

	public JPanel createHorizontalPanel(boolean threeD) {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
		p.setAlignmentY(JFrame.TOP_ALIGNMENT);
		p.setAlignmentX(JFrame.LEFT_ALIGNMENT);
		if(threeD) {
			p.setBorder(loweredBorder);
		}
		return p;
	}
	
	public JPanel createVerticalPanel(boolean threeD) {
		JPanel p = new JPanel();
		p.setLayout(new BoxLayout(p, BoxLayout.Y_AXIS));
		p.setAlignmentY(JFrame.TOP_ALIGNMENT);
		p.setAlignmentX(JFrame.LEFT_ALIGNMENT);
		if(threeD) {
			p.setBorder(loweredBorder);
		}
		return p;
	}

	public static void main(String[] args) {
		Viewer viewer = new Viewer(null);
		viewer.init();
	}
	
	public void init() {
		setViewerFrame(new JFrame());
		getViewerFrame().setIconImage(getImageIcon());
		getViewerFrame().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		getViewerPanel().setPreferredSize(new Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
		getViewerFrame().getContentPane().setLayout(new BorderLayout());
//		getViewerFrame().getContentPane().add(getViewerPanel(), BorderLayout.CENTER);
		getViewerFrame().setContentPane(getViewerPanel());
		getViewerFrame().pack();
		getViewerFrame().setVisible(true);
   }
}